﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cnm.Core;
using Cnm.Core.Ultities;
using Cnm.Models;
using Cnm.Models.Functions;
using Cnm.Models.ViewModels;
using Cnm.Servicers.Cacher;
using Cnm.Servicers.Seo;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

namespace Cnm.Mvc.Web.Controllers
{
    public class RefController : Controller
    {
        CnmDemoEntities dbContext = new CnmDemoEntities();
        private ICacher _cacher;
        public RefController(ICacher cacher)
        {
            this._cacher = cacher;
        }

        public ActionResult ReturnJsonForGrid(IEnumerable model, int total, [DataSourceRequest]DataSourceRequest request)
        {
            //request.Page = 1;
            var result = model.ToDataSourceResult(request);
            //result.Data
            result.Total = total;
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetFirstLevelCategories()
        {
            return Json(_cacher.GetFirstLevelCategories(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSecondLevelCategories(int parentId)
        {
            return Json(_cacher.GetSecondLevelCategories(parentId), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetAllLevelCategories()
        {
            return Json(_cacher.GetAllLevelCategories(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetRegions()
        {
            return Json(_cacher.GetRegions(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetItemStatus()
        {
            return Json(_cacher.GetItemStatus(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPaymentMethods()
        {
            return Json(_cacher.GetPaymentMethods(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetPostTypes(int? categoryId)
        {
            return Json(_cacher.GetPostTypes(categoryId), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetTitulars()
        {
            return Json(_cacher.GetTitulars(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetPosts(
            [DataSourceRequest] DataSourceRequest request, 
            int? titularId, 
            int? categoryId, 
            int? regionId,
            int? postTypeId,
            int? itemStatusId,
            string keyword = "")
        {
            var models = dbContext.Posts.Where(p=>p.Published != false);
            if (titularId != null)
                models = models.Where(p => p.TitularId == titularId);
            if (categoryId != null)
                models = models.Where(p =>
                    p.CategoryId == categoryId
                    || (p.Category != null && p.Category.ParentId == categoryId));
            if (regionId != null)
                models = models.Where(p => p.RegionId == regionId);
            if (postTypeId != null)
                models = models.Where(p => p.PostTypeId == postTypeId);
            if (itemStatusId != null)
                models = models.Where(p => p.ItemStatusId == itemStatusId);
            if (keyword != "" && !string.IsNullOrWhiteSpace(keyword))
            {
                keyword = keyword.Trim();
                keyword = CommonHelpers.RemoveVietnameseSign(keyword);
                models = models
                    .Where(p => CommonFunctions.ToCleanVietnameseSign(p.Name).Contains(keyword));
            }
            var viewModels = models
                .OrderByDescending(p=>p.DateCreated)
                .ToList()
                .Select(p => new PostOverviewModel
                {
                    Name = p.Name,
                    PriceString = string.Format("{0:0,0}",p.Price),
                    DateCreated = p.DateCreated,
                    Region = new RegionViewModel
                    {
                        Name = p.Region.Name,
                        SeName = p.Region.GetSeName(),
                        UrlDetails = Url.RouteUrl("Region", new { SeName = p.Region.GetSeName() })
                    },
                    Category = new CategoryViewModel
                    {
                        Name = p.Category.Name,
                        SeName = p.Category.GetSeName(),
                        UrlDetails = Url.RouteUrl("Category", new { SeName = p.Category.GetSeName() })
                    },
                    SeName = p.GetSeName(),
                    ImageName = p.Post_Image != null && p.Post_Image.FirstOrDefault() != null
                        ? p.Post_Image.FirstOrDefault().Image.FileName
                        : "not_found.png",

                    UrlDetails = Url.RouteUrl("Post", new { SeName = p.GetSeName() })
                });

            var dataSourceModels = viewModels.ToDataSourceResult(request);

            return Json(dataSourceModels, JsonRequestBehavior.AllowGet);
        }
	}
}